//
//  DemoViewController.m
//
//  Created by 周成龙
//

#import "DemoViewController.h"

#define FONT(a) [UIFont systemFontOfSize:a]
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kHeaderHeight 35
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]

@interface DemoViewController ()<UITextViewDelegate>{
    //意见内容
    UITextView *contentTextView;
    //在UITextView上面覆盖个UILable
    UILabel *promptLabel;
}

@end

@implementation DemoViewController
#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    //意见内容
    
    
    contentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, kHeaderHeight, kScreenWidth, 86)];
    contentTextView.font = FONT(13);
    contentTextView.delegate = self;
    [self.view addSubview:contentTextView];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 0, 60, 35)];
    contentLabel.font = FONT(13);
    contentLabel.text = @"*您的意见";
    [contentTextView addSubview:contentLabel];
    
    //在UITextView上面覆盖个UILable
    promptLabel = [[UILabel alloc] init];
    promptLabel.frame =CGRectMake(5,5,200,25);
    promptLabel.text = @"                         请输入意见";
    promptLabel.enabled = NO;
    promptLabel.backgroundColor = [UIColor clearColor];
    promptLabel.font =  [UIFont systemFontOfSize:13];
    promptLabel.textColor = RGB(245, 245, 245);
    [contentTextView addSubview:promptLabel];
    
    //改变五角星的颜色
    NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    [contentStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
    contentLabel.attributedText = contentStr;
    
    [self textViewDidChange:contentTextView];
}
#pragma mark -UITextView的代理方法
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        promptLabel.hidden = NO;
    }else{
        promptLabel.hidden = YES;
    }
    
    
    //首行缩进
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 3;    //行间距
    //    paragraphStyle.maximumLineHeight = 60;   /**最大行高*/
    paragraphStyle.firstLineHeadIndent = 93.f;    /**首行缩进宽度*/
    paragraphStyle.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:13],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}

@end
